import threading

import zmq

def resp_client():
    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    socket.connect("tcp://localhost:5555")

    for i in range(10):
        socket.send("{}".format(i).encode())
        response = socket.recv()
        print("Received response %s [%s]" % (i, response))
    socket.send(b"Exit")

def req_server():
    context = zmq.Context()
    socket = context.socket(zmq.REP)
    socket.bind("tcp://*:5555")

    while True:
        message = socket.recv()
        print("Received request: %s" % message.decode())
        socket.send(b"ACK")
        if message.decode() == "Exit":
            socket.close()
            context.term()
            return


if __name__ == "__main__":
    server_thread = threading.Thread(target=req_server)
    server_thread.start()
    client_thread = threading.Thread(target=resp_client)
    client_thread.start()
    client_thread.join()

    server_thread.join()
